﻿using System;
using System.Collections.Generic;

namespace AppMain
{
    /// <summary>
    /// 受击 曲线位移
    /// </summary>
    public class TMHitCurveProcesser : BaseMoveProcesser
    {
        protected override void onEnter(RoleMove_Status fromStatus)
        {
            this.agent.box3D.Transverse = true;
            this.agent.RoleHeight = 0.5f;
            cacluteBezier();
        }

        protected override void onUpdate()
        {
            cacluteBezier();
        }

        protected override void onExit(RoleMove_Status toStatus)
        {

        }

        protected Vec3 P0;
        protected Vec3 P1;
        protected Vec3 P2;
        protected float timeScale = 1;

        protected float t = 0;
        protected float reboundValue = 1.5f;
        protected float reboundScale = 1f;
        protected float maxH = 0;
        protected virtual void cacluteBezier()
        {
            this.agent.Lock((int)Lock_Status.MoveLock);
            maxH = 0;
            t = 0;
            //贝塞尔曲线
            float xdis = this.data.distance * this.data.dir.x;
            float zdis = this.data.distance * this.data.dir.z;
            BaseScene scene = this.agent.AttrMap.getAttr<BaseScene>(RoleAttr.Scene).Value;
            Vec2 result = Physics.moveCast(this.agent.box3D, scene.colliderSegments, xdis, zdis);
            float validDistanceX = result.x;
            float validDistanceZ = result.y;

            //P2 = 终点
            P2 = new Vec3();
            P2.x = this.agent.box3D.posX + validDistanceX;
            P2.y = 0;
            P2.z = this.agent.box3D.posZ + validDistanceZ;
            //P0 = 起点
            P0 = new Vec3();
            P0.x = this.agent.box3D.posX;
            P0.y = this.agent.RoleHeight;
            P0.z = this.agent.box3D.posZ;
            //P1 = 取中心
            P1 = new Vec3();
            P1.x = (P0.x + P2.x) * 0.5f;
            P1.y = this.agent.RoleHeight + this.data.height;
            P1.z = (P0.z + P2.z) * 0.5f;
            timeScale = this.data.time <= 0 ? 1 : 1 / this.data.time;
        }

        protected override void onTick(float frameTime)
        {
            t += frameTime * (this.timeScale);
            if (t >= 1)
                t = 1;

            Vec3 result = (1 - t) * (1 - t) * P0 + 2 * t * (1 - t) * P1 + t * t * P2;            
            this.agent.RoleHeight = result.y;
            maxH = result.y > maxH ? result.y : maxH;
            this.agent.movePos(result.x, result.z);

            if (t >= 1)
            {
                onMoveFinish();
            }
        }

        protected virtual void onMoveFinish()
        {
            if (maxH > reboundValue)
            {
                this.data.height = maxH * reboundScale;
                this.data.distance = this.data.distance * reboundScale;
                this.agent.moveWidget.forceTo(RoleMove_Status.HitCurveRebound, this.data);
            }
            else
            {
                this.agent.moveWidget.forceTo(RoleMove_Status.LieDown);
            }
        }


        protected override void onDispose()
        {

        }
    }
}
